package com.coldwindblows.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coldwindblows.blog.dao.AttachMapper;
import com.coldwindblows.blog.pojo.AttachDo;
import com.coldwindblows.blog.service.IAttachService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class IAttachServiceImpl implements IAttachService {
	
	@Autowired
	private AttachMapper attachDao;
	
	@Override
	public PageInfo<AttachDo> getAttachs(int page, int pageSize) {
		PageHelper.startPage(page, pageSize);
		List<AttachDo> list = attachDao.listAll();
		
		return new PageInfo<>(list);
	}

	@Transactional
	@Override
	public boolean insertAttach(String fname, String fkey, String ftype, Integer authorId) {
		AttachDo attach = new AttachDo();
		attach.setFname(fname);
		attach.setAuthorId(authorId);
		attach.setFkey(fkey);
		attach.setFtype(ftype);
		attach.setCreated(new Date());
		
		return attachDao.insert(attach) > 0;
	}

	@Override
	public AttachDo getDetail(Integer id) {
		if(id == null) {
			return null;
		}
		return attachDao.selectByPrimaryKey(id);
	}

	@Transactional
	@Override
	public boolean deleteAttach(Integer id) {
		if(id == null) {
			return false;
		}
		return attachDao.deleteByPrimaryKey(id) > 0;
	}

}
